package com.xykj.enterprise.wechat.ext.api.contacts.member;

import com.xykj.enterprise.wechat.bean.ext.BaseResp;
import com.xykj.enterprise.wechat.ext.service.contacts.ContactsUserService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Function(description = "创建成员", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "userid", type = Type.String, description = "账号", required = true),
        @Parameter(name = "name", type = Type.String, description = "姓名", required = false),
        @Parameter(name = "department", type = Type.String, description = "部门", required = false),
        @Parameter(name = "order", type = Type.String, description = "排序值", required = false),
        @Parameter(name = "position", type = Type.String, description = "职位", required = false),
        @Parameter(name = "mobile", type = Type.String, description = "手机号", required = false),
        @Parameter(name = "gender", type = Type.String, description = "性别", required = false),
        @Parameter(name = "email", type = Type.String, description = "邮箱", required = false),
        @Parameter(name = "is_leader_in_dept", type = Type.String, description = "是否负责人", required = false),
        @Parameter(name = "enable", type = Type.Integer, description = "是否可用", required = false),
        @Parameter(name = "avatar_mediaid", type = Type.String, description = "头像", required = false),
        @Parameter(name = "telephone", type = Type.String, description = "座机", required = false),
        @Parameter(name = "alias", type = Type.String, description = "别名", required = false),
        @Parameter(name = "address", type = Type.String, description = "地址", required = false),
        @Parameter(name = "main_department", type = Type.Integer, description = "主部门", required = false),
})
@Component
public class UpdateUser implements Action {

    @Autowired
    private ContactsUserService contactsUserService;

    @Override
    public Response execute(Request request) throws Exception {

        String userid = request.getString("userid");
        String name = request.getString("name");
        String department = request.getString("department");
        String order = request.getString("order");
        String position = request.getString("position");
        String mobile = request.getString("mobile");
        String gender = request.getString("gender");
        String email = request.getString("email");
        String is_leader_in_dept = request.getString("is_leader_in_dept");
        Integer enable = request.getInteger("enable");
        String avatar_mediaid = request.getString("avatar_mediaid");
        String telephone = request.getString("telephone");
        String alias = request.getString("alias");
        String address = request.getString("address");
        Integer main_department = request.getInteger("main_department");
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("userid", userid);
        params.put("name", name);
        if (department != null) {
            String[] departmentSplit = department.split(",");
            params.put("department", departmentSplit);
        }
        if (order != null) {
            String[] orderSplit = order.split(",");
            params.put("order", orderSplit);
        }
        params.put("position", position);
        params.put("mobile", mobile);
        params.put("gender", gender);
        params.put("email", email);
        params.put("is_leader_in_dept", is_leader_in_dept);
        params.put("enable", enable);
        params.put("avatar_mediaid", avatar_mediaid);
        params.put("telephone", telephone);
        params.put("alias", alias);
        params.put("address", address);
        params.put("main_department", main_department);
        if (params.size() <= 1) {
            return Response.fail("参数错误！");
        }
        BaseResp BaseResp = contactsUserService.updateUser(
                request.getString("access_token"), params
        );
        if (BaseResp.getErrcode() != 0) {
            return Response.fail(BaseResp.getErrmsg());
        }
        return Response.success();
    }
}
